/**
 * ReportGear(2011)
 */
package com.reportgear.designer.pane.style;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import com.reportgear.core.swing.pane.base.JBasePane;
import com.reportgear.designer.ui.base.JIdNameComboBox;
import com.reportgear.report.model.cell.Cell;
import com.reportgear.report.model.cell.style.HorizentalAlignment;
import com.reportgear.report.model.cell.style.Style;
import com.reportgear.report.model.cell.style.TextStyle;
import com.reportgear.report.model.cell.style.VerticalAlignment;
import com.reportgear.report.view.JWorksheet;

/**
 * @version 1.0 2011-3-24
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class AlignPane extends JBasePane implements ICellStyle {

	private JIdNameComboBox<HorizentalAlignment> cbxHAlign;
	private JIdNameComboBox<VerticalAlignment> cbxVAlign;
	private JIdNameComboBox<TextStyle> cbxTextStyle;

	public AlignPane() {
		super();
		
		initComponents();
	}

	private void initComponents() {
		setLayout(new BorderLayout());
		JPanel pane1 = new JPanel();
		pane1.setLayout(new GridLayout(1, 3, 2, 2));

		JPanel paneHAlign = new JPanel();
		paneHAlign.setLayout(new BorderLayout());
		JPanel paneVAlign = new JPanel();
		paneVAlign.setLayout(new BorderLayout());
		JPanel paneTextStyle = new JPanel();
		paneTextStyle.setLayout(new BorderLayout());

		paneHAlign.setBorder(BorderFactory.createTitledBorder("水平对齐"));
		cbxHAlign = new JIdNameComboBox<HorizentalAlignment>(HorizentalAlignment.getIdNameList().toArray());
		paneHAlign.add(cbxHAlign, BorderLayout.CENTER);

		paneVAlign.setBorder(BorderFactory.createTitledBorder("垂直对齐"));
		cbxVAlign = new JIdNameComboBox<VerticalAlignment>(VerticalAlignment.getIdNameList().toArray());
		paneVAlign.add(cbxVAlign, BorderLayout.CENTER);

		paneTextStyle.setBorder(BorderFactory.createTitledBorder("文本控制"));
		cbxTextStyle = new JIdNameComboBox<TextStyle>(TextStyle.getIdNameList().toArray());
		paneTextStyle.add(cbxTextStyle, BorderLayout.CENTER);

		pane1.add(paneHAlign);
		pane1.add(paneVAlign);
		pane1.add(paneTextStyle);

		add(pane1, BorderLayout.NORTH);
	}

	public void populateCellStyle(JWorksheet workSheet) {
		Style style = workSheet.getActiveCellStyle();

		this.cbxHAlign.setIndexByValueId(style.getHorizentalAlignment());
		this.cbxVAlign.setIndexByValueId(style.getVerticalAlignment());
		this.cbxTextStyle.setIndexByValueId(style.getTextStyle());
	}

	public void updateCellStyle(JWorksheet workSheet) {
		HorizentalAlignment hAlign = this.cbxHAlign.getSelectValueId();
		VerticalAlignment vAlign = this.cbxVAlign.getSelectValueId();
		TextStyle textStyle = this.cbxTextStyle.getSelectValueId();
		List<Cell> cells = workSheet.getSelectedCells();
		Style style = workSheet.getActiveCellStyle();
		if (style.getHorizentalAlignment() != hAlign) {
			for (Cell cell : cells) {
				cell.setStyle(cell.getStyle().derivetHorizentalAlignment(hAlign));
			}
		}

		if (style.getVerticalAlignment() != vAlign) {
			for (Cell cell : cells) {
				cell.setStyle(cell.getStyle().derivetVerticalAlignment(vAlign));
			}
		}

		if (style.getTextStyle() != textStyle) {
			for (Cell cell : cells) {
				cell.setStyle(cell.getStyle().derivetTextStyle(textStyle));
			}
		}
	}
}
